package com.dartfar.permission.service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.annotation.security.RolesAllowed;

import org.springframework.stereotype.Component;

import com.dartfar.base.model.Authority;
import com.dartfar.base.service.BaseServiceImpl;
import com.dartfar.menu.model.Menu;
import com.dartfar.permission.model.Permission;

@Component("permissionService")
public class PermissionServiceImpl extends BaseServiceImpl<Permission> implements PermissionService {

	/**
	 * 增加权限
	 * @param roleId
	 * @param menuId
	 * @param funIds
	 */
	@Override
	public void add(String roleId, String menuId, String funIds){
		Permission permission = new Permission();
		permission.setRoleId(roleId);
		permission.setMenuId(menuId);
		permission.setFunIds(funIds);
		save(permission);
	}
	
	/**
	 * 根据角色ID删除权限
	 * @param roleId
	 */
	@Override
	public void removeByRoleId(String roleId) {
		// TODO Auto-generated method stub
		String hql = "delete from Permission p where p.roleId = '" + roleId + "'";
		execute(hql);
	}

	/**
	 * 根据角色ID或者菜单ID获取权限
	 * @param roleIds
	 * @param menuId
	 * @return
	 */
	@Override
	public List<Permission> getListByRoleIdOrMenuId(String roleIds, String menuIds) {
		// TODO Auto-generated method stub
		String hql = "from Permission p where 1 = 1";
		if(roleIds != null){
			hql = hql + " and p.roleId in ('" + roleIds + "')";
		}
		if(menuIds != null){
			hql = hql + " and p.menuId in ('" + menuIds + "')";
			//hql = hql + " and p.menuId = '" + menuId + "'";
		}
		hql = hql + " order by p.id asc";
		return getList(hql);
	}


}
